<?php

Class EmailTemplatesController extends AppController
{
	var $name = 'EmailTemplates';
	var $uses = array('Cms.EmailTemplate');
	var $helpers = array('Form');
	function admin_index()
	{
		$this->pageTitle = __("Email Templates");
		$emailTemplates  = $this->EmailTemplate->find('all',array("order"=>"name ASC"));
		$this->set('emailTemplates',$emailTemplates);
	}

	function admin_edit($id=null)
	{
		uses('L10n');
		$L10 = new L10n();
		$languages =($L10->__l10nCatalog);
		
		if(!$id || !$emailTemplate = $this->EmailTemplate->findById($id))
		{
			$this->errorMsg(__('Invalid id',true));
			$this->redirect(array('action'=>'index'));
		}
		
		if($this->data)
		{		
			$presentLanguageName = Configure::read('Config.language');
			$content = $this->data['EmailTemplate']['body_html'];
			$this->data['EmailTemplate']['id'] = $id;
			if($this->EmailTemplate->addSameTemplate($content,$this->data['EmailTemplate']))
			{
				$this->successMsg(__('Email Template has been saved',true));
				$this->redirect(array('action'=>'index'));
			}
		}else
		{
			$this->data = $emailTemplate;
		}
		$this->pageTitle = sprintf(__('Edit email template for %s',true),__(Inflector::humanize($emailTemplate['EmailTemplate']['name'])));
		$this->set(compact('templateKeys','emailTemplate'));
	}
	
	function admin_restorDefault($name)
	{
		$fileName = 'template_'.$name.'.ctp';
		$filePath = WWW_ROOT.'files'.DS.$fileName;
		if(!file_exists($filePath))
		{
			$this->redirect($this->referer());
		}
		$mailSubject = Configure::read('emailConfig.MailConfiguration.subject_'.$name);
		if(is_null($mailSubject))
			$mailSubject = '';
		$content = file_get_contents($filePath);
		$id = $this->EmailTemplate->field('id',array('name'=>$name));
		$data['EmailTemplate'] =  array('id'=>$id,'body_text'=>$content,'body_html'=>$content,'subject'=>$mailSubject,'name'=>$name);
		$this->EmailTemplate->create();
		$this->EmailTemplate->save($data,array('validate'=>false,'callbacks'=>false));
		$this->successMsg(__('Email template has been restored to default',true));
		$this->redirect($this->referer());
	}
}
?>